package com.tangbuzhi.composite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 组合模式案例
 */
public class CompositeCase {
    public static abstract class Dir {
        protected List<Dir> dirs = new ArrayList<>();
        private String name;

        public Dir(String name) {
            this.name = name;
        }

        public abstract void addDir(Dir dir);

        public abstract void removeDir(Dir dir);

        public abstract void clear();

        public abstract void show();

        public abstract List<Dir> getDirs();

        public String getName() {
            return name;
        }
    }

    public static class Folder extends Dir {
        public Folder(String name) {
            super(name);
        }

        @Override
        public void addDir(Dir dir) {
            dirs.add(dir);
        }

        @Override
        public void removeDir(Dir dir) {
            dirs.remove(dir);
        }

        @Override
        public void clear() {
            dirs.clear();
        }

        @Override
        public void show() {
            System.out.print(getName() + "(");
            Iterator<Dir> iterator = dirs.iterator();
            while (iterator.hasNext()) {
                Dir dir = iterator.next();
                dir.show();
                if (iterator.hasNext()) {
                    System.out.print(",");
                }
            }
            System.out.print(")");
        }

        @Override
        public List<Dir> getDirs() {
            return dirs;
        }
    }

    public static class File extends Dir {
        public File(String name) {
            super(name);
        }

        @Override
        public void addDir(Dir dir) {
            throw new UnsupportedOperationException("File not support add dir");
        }

        @Override
        public void removeDir(Dir dir) {
            throw new UnsupportedOperationException("File not support remove dir");
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException("File not support clear dir");
        }

        @Override
        public void show() {
            System.out.print(getName());
        }

        @Override
        public List<Dir> getDirs() {
            throw new UnsupportedOperationException("File not support get dir");
        }
    }

    public static void main(String[] args) {
        Folder cDisk = new Folder("C");
        cDisk.addDir(new File("ImbaMallLog.txt"));

        Folder user = new Folder("user");
        Folder admin = new Folder("admin");
        admin.addDir(new File("download"));
        user.addDir(admin);
        user.addDir(new Folder("public"));
        cDisk.addDir(user);

        Folder windows = new Folder("Windows");
        windows.addDir(new File("windows.dll"));
        cDisk.addDir(windows);

        Folder program_file = new Folder("Program Files");
        program_file.addDir(new File("mail"));
        cDisk.addDir(program_file);

        Folder prefer_log = new Folder("PrefLogs");
        prefer_log.addDir(new File("log.txt"));
        cDisk.addDir(prefer_log);

        cDisk.show();
        /**
         C(ImbaMallLog.txt,user(admin(download),public()),Windows(windows.dll),Program Files(mail),PrefLogs(log.txt))
         */
    }
}
